package com.ruimei.service.impl;

import com.alibaba.fastjson.JSON;
import com.ruimei.entitys.*;
import com.ruimei.mapper.AttendanceMapper;
import com.ruimei.mapper.EvaluateMapper;
import com.ruimei.service.EvaluateService;
import com.ruimei.util.postUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EvaluateServiceImpl implements EvaluateService {

    @Resource
    EvaluateMapper evaluateMapper;

    @Resource
    RedisTemplate<String,String> redisTemplate;

    @Resource
    AttendanceMapper attendanceMapper;

    @Override
    public Map<String, Object> getEvaluate(String classId, String studentName,String evaluateId,String teacherId) {
        Map<String, Object> map = new HashMap();

        //查询教师权限


        if(postUtil.post(redisTemplate,teacherId,attendanceMapper)){
            teacherId = null;
        }
        List<Evaluate> list = evaluateMapper.getEvaluate(classId,studentName,evaluateId,teacherId);

        map.put("status", Boolean.valueOf(true));
        map.put("message", "查询成功");
        map.put("count", Integer.valueOf(list.size()));
        map.put("data", list);
        return map;

    }

    @Override
    public Map<String, Object> getEvaluateP(String studentId,String evaluateId) {
        Map<String, Object> map = new HashMap();
        List<Evaluate> list = evaluateMapper.getEvaluateP(studentId);

        map.put("status", Boolean.valueOf(true));
        map.put("message", "查询成功");
        map.put("count", Integer.valueOf(list.size()));
        map.put("data", list);
        return map;

    }

    @Override
    public Map<String, Object> getParentComment(String title,String studentId) {
        Map<String, Object> map = new HashMap();
        List<ParentComment> list = evaluateMapper.getParentComment(title,studentId);

        map.put("status", Boolean.valueOf(true));
        map.put("message", "查询成功");
        map.put("count", Integer.valueOf(list.size()));
        map.put("data", list);
        return map;

    }

    @Override
    public Map<String, Object> addEvaluate(Evaluate evaluate) {
        Map<String, Object> map = new HashMap();
        List<Evaluate> list = evaluateMapper.checkEvaluate(evaluate);
        if (list.size() == 0) {
            if (evaluate.getContent().isEmpty()) {
                evaluate.setContent("");
            }
            Integer i = evaluateMapper.addEvaluate(evaluate);
            if (i.intValue() == 1) {
                map.put("status", Boolean.valueOf(true));
                map.put("message", "添加成功");
            } else {
                map.put("status", Boolean.valueOf(false));
                map.put("message", "添加失败");
            }
        }else{
            map.put("status", Boolean.valueOf(false));
            map.put("message", "不能重复评价");
        }

        return map;
    }

    @Override
    public Map<String, Object> setEvaluateT(Evaluate evaluate,String id) {
        Map<String, Object> map = new HashMap();
        if(id.equals(evaluate.getTeacherId())){
            Integer i = evaluateMapper.setEvaluateT(evaluate);
            if(i.intValue() == 1){
                map.put("status", Boolean.valueOf(true));
                map.put("message", "添加成功");
            }else{
                map.put("status", Boolean.valueOf(false));
                map.put("message", "添加失败");
            }
        }else{
            map.put("status", Boolean.valueOf(false));
            map.put("message", "只能修改自己的评价");
        }


        return map;
    }

    @Override
    public Map<String, Object> setParentComment(ParentComment parentComment) {
        Map<String, Object> map = new HashMap();
        List<ParentComment> list = evaluateMapper.getParentComment(parentComment.getTitle(),parentComment.getStudentId());
        Integer i;
        if(list.isEmpty()){
            i = evaluateMapper.addParentComment(parentComment);
        }else{
            i = evaluateMapper.setParentComment(parentComment);
        }


        if(i.intValue() == 1){
            map.put("status", Boolean.valueOf(true));
            map.put("message", "添加成功");
        }else{
            map.put("status", Boolean.valueOf(false));
            map.put("message", "添加失败");
        }

        return map;
    }

    @Override
    public Map<String, Object> deleteEvaluate(String evaluateId) {
        Map<String, Object> map = new HashMap();

        Integer i = evaluateMapper.deleteEvaluate(evaluateId);
        if(i.intValue() == 1){
            map.put("status", Boolean.valueOf(true));
            map.put("message", "添加成功");
        }else{
            map.put("status", Boolean.valueOf(false));
            map.put("message", "添加失败");
        }

        return map;

    }
}
